﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmUpdatePositionList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int department_ID = 0;
        int branch_ID = 0;
        MainServiceClient wcfMain;
        EmployeeClient wcfEmployee;
        RoleRightInfo listRole;

        public frmUpdatePositionList()
        {
            InitializeComponent();
            LoadLanguage();
            wcfEmployee = Service.Employee();
            wcfMain = Service.MainService();

            listRole = UIParamater.Quyen_CanhBaoNangLuong;

            if (!listRole.Update)
            {
                this.btnViewEmloyee.Image = global::HRM.Properties.Resources.app;
                this.btnViewEmloyee.Tag = "xem";
            }
            txtToDate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            cboMonth.SelectedIndex = 1;
            BuildVoucherGrid();
            UISetting.LoadTreeView3(ref tvBranch);
            this.KeyPreview = true;

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "", 50, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Employee_Code", UpdateUI.GetString("maso"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Grade_Name", UpdateUI.GetString("capdo"), "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "PD_BasicFactor", UpdateUI.GetString("hesoluongcoban"), "R", 150, true, true);
            //UISetting.AddColumn(ref dgvList, "PD_BeginDate", UpdateUI.GetString("ngayxetlantruoc"), "C", 130, true, true);
            //UISetting.AddColumn(ref dgvList, "PD_Month", UpdateUI.GetString("sothangtang"), "R", 130, true, true);
            UISetting.AddColumn(ref dgvList, "BRANCH_NAME", UpdateUI.GetString("chinhanh"), "", 200, true, true);
            UISetting.AddColumn(ref dgvList, "DEPARTMENT_NAME", UpdateUI.GetString("phongban"), "", 200, true, true);
        }
        private void LoadData()
        {
            int _dayVal = 0;
            if (cboMonth.Text.Length > 0)
                _dayVal = Convert.ToInt32(cboMonth.Text);
            DateTime _todate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
            DataSet _ds = wcfEmployee.Employee_IncreaseSalary(out itemCount, _dayVal, branch_ID, department_ID, _todate, currPage, pageSize, 2);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_LastName"].ToString()
                    + " " + _ds.Tables[0].Rows[i]["Employee_FirstName"].ToString();
                dgvList.Rows[i].Cells["Grade_Name"].Value = _ds.Tables[0].Rows[i]["Grade_Name"].ToString();
                dgvList.Rows[i].Cells["PD_BasicFactor"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["PD_BasicFactor"]).ToString("N1");
                //dgvList.Rows[i].Cells["PD_BeginDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["PD_BeginDate"]).ToString("dd/MM/yyyy");
                //dgvList.Rows[i].Cells["PD_Month"].Value = _ds.Tables[0].Rows[i]["PD_Month"].ToString();
                dgvList.Rows[i].Cells["BRANCH_NAME"].Value = _ds.Tables[0].Rows[i]["BRANCH_NAME"].ToString();
                dgvList.Rows[i].Cells["DEPARTMENT_NAME"].Value = _ds.Tables[0].Rows[i]["DEPARTMENT_NAME"].ToString();


            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            string _month = cboMonth.Text;
            string _todate = txtToDate.Text;

            frmPreviewReport f = new frmPreviewReport("DSCB_SapLenLuong", new string[] { _month, _todate, branch_ID.ToString(), department_ID.ToString() });
            f.Show(this);
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
            if (dgvList.Rows.Count > 0)
                btnPrint.Enabled = true;
            else
                btnPrint.Enabled = false;
        }

        private void btnViewEmloyee_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                int Employee_ID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                Form f = new frmEmployeeInfo(Employee_ID,0, 0, UIParamater.Employee_Active, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    branch_ID = 0;
                    department_ID = 0;
                }
                else
                    branch_ID = Program.intBranchID;
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();

            }
            else
                department_ID = 0;
            currPage = 0;
            LoadData();
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void frmIncreaseSalaryList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("maso");
                dgvList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvList.Columns["Grade_Name"].HeaderText = UpdateUI.GetString("capdo");
                dgvList.Columns["PD_BasicFactor"].HeaderText = UpdateUI.GetString("hesoluongcoban");
                //dgvList.Columns["PD_BeginDate"].HeaderText = UpdateUI.GetString("ngayxetlantruoc");
                //dgvList.Columns["PD_Month"].HeaderText = UpdateUI.GetString("sothangtang");
            }
        }

        private void frmIncreaseSalaryList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfMain.Close();
        }
    }
}
